<?php

namespace App\Service\queue;
use App\Job\WxDramaJob;
use App\Job\DyDramaJob;
use App\Job\WxReplaceDramaJob;
use App\Job\WxReplaceMediaJob;
use App\Model\Applet;
use App\Service\CommicService;
use Hyperf\Utils\ApplicationContext;
use App\Job\WxMediaJob;
use App\Job\DyMediaJob;
use Hyperf\AsyncQueue\Driver\DriverFactory;
use Hyperf\AsyncQueue\Driver\DriverInterface;
class DramaService extends CommicService
{
    //获取tokenurl
    public static $getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token";
    //上传媒资视频接口-拉取上传

    protected DriverInterface $driver;

    public function __construct(DriverFactory $driverFactory)
    {
        $container = ApplicationContext::getContainer();
        $this->redisService = $container->get(\Hyperf\Redis\Redis::class);
        $this->driver = $driverFactory->get('default');
    }
    /**
     * 获取token
     */
    public function getWxAccessToken($applet_id){
        $key = 'wx_access_token';
        $key .= $applet_id;
        $accessToken = $this->redisService->get($key);
        if(empty($accessToken)){
            $wxAppid = '';
            $wxSecret = '';
            # 获取小程序基本信息
            $appletInfo = Applet::query()->where(["id"=>$applet_id])->first()->toArray();
            if(!empty($appletInfo)){
                $wxAppid = $appletInfo['app_id'];
                $wxSecret = $appletInfo['app_secret'];
            }

            $params         = [
                'grant_type' => 'client_credential',
                'appid'    => $wxAppid,
                'secret'   => $wxSecret
            ];
            //获取token
            $accessTokenInfo     = $this->request($params, self::$getTokenUrl,'GET');

            if (!empty($accessTokenInfo['access_token'])){
                $accessToken = $accessTokenInfo['access_token'];
                $this->redisService->setex($key,7100,$accessToken);
            }else{
                $accessToken = "";
            }
        }
        return $accessToken;
    }
    /**
     * 获取短剧的信息
     */
    public function getWxDramaInfo(int $drama_id,int $applet_id){
        $accessToken = $this->getWxAccessToken($applet_id);
        if(!empty($accessToken)){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 上传视频到微信媒资
     */
    public function vodPullUpolad(Array $playletEpisodesIds,$type=1){

        if($type == 1){
            $this->driver->push(new WxMediaJob($playletEpisodesIds));
        }else{
            $this->driver->push(new DyMediaJob($playletEpisodesIds));
        }
    }
    /**
     * 提审剧目
     */
    public function auditdrama(int $playet_id,int $applet_id, int $type=1){

        if($type == 1){
            $this->driver->push(new WxDramaJob($playet_id,10000));
        }else{
            var_dump($playet_id,$applet_id);
            $this->driver->push(new DyDramaJob($playet_id,$applet_id));
        }
    }

    public function test(){


        $this->driver->push(new WxReplaceDramaJob(29,[1009]));


//        $this->driver->push(new WxReplaceMediaJob(29,[1009]));
    }

}